// FeeQueryPanel.java 费用查询界面
package com.dw.gui.panel.fee;

import com.dw.dao.invoice.InvoiceDao;
import com.dw.model.invoice.Invoice;
import com.dw.model.user.Doctor;
import com.dw.util.UIUtil;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class FeeQueryPanel extends JPanel {
    private JTextField tfPatientId;
    private JTextField tfInvoiceNo;
    private JTextField tfStartDate;
    private JTextField tfEndDate;
    private JComboBox<String> cbFeeType;
    private JTable resultTable;

    public FeeQueryPanel(Doctor doctor) {
        initUI();
        loadInitialData();
    }

    private void initUI() {
        setLayout(new BorderLayout(10, 10));
        setBackground(Color.WHITE);

        // 标题面板
        JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        titlePanel.setBackground(Color.WHITE);
        titlePanel.add(UIUtil.createTitleLabel("费用查询"));
        titlePanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // 查询条件面板
        JPanel queryPanel = new JPanel(new GridLayout(0, 4, 10, 10));
        queryPanel.setBackground(Color.WHITE);
        queryPanel.setBorder(BorderFactory.createTitledBorder("查询条件"));

        queryPanel.add(UIUtil.createLabel("患者ID："));
        tfPatientId = UIUtil.createTextField();
        queryPanel.add(tfPatientId);

        queryPanel.add(UIUtil.createLabel("发票号："));
        tfInvoiceNo = UIUtil.createTextField();
        queryPanel.add(tfInvoiceNo);

        queryPanel.add(UIUtil.createLabel("开始日期："));
        tfStartDate = UIUtil.createTextField();
        queryPanel.add(tfStartDate);

        queryPanel.add(UIUtil.createLabel("结束日期："));
        tfEndDate = UIUtil.createTextField();
        queryPanel.add(tfEndDate);

        queryPanel.add(UIUtil.createLabel("费用类型："));
        cbFeeType = UIUtil.createComboBox(new String[]{"全部", "收费", "退费"});
        cbFeeType.setSelectedItem("收费"); // 设置默认选中收费
        queryPanel.add(cbFeeType);

        // 操作按钮
        JButton btnSearch = UIUtil.createButton("查询");
        btnSearch.addActionListener(_ -> performSearch());
        JButton btnReset = UIUtil.createButton("重置");
        btnReset.addActionListener(e -> resetCriteria());

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
        buttonPanel.setBackground(Color.WHITE);
        buttonPanel.add(btnSearch);
        buttonPanel.add(btnReset);

        // 结果表格
        resultTable = new JTable();
        resultTable.setAutoCreateRowSorter(true);
        JScrollPane scrollPane = new JScrollPane(resultTable);

        // 主内容面板
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        mainPanel.add(queryPanel, BorderLayout.NORTH);
        mainPanel.add(scrollPane, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        add(titlePanel, BorderLayout.NORTH);
        add(mainPanel, BorderLayout.CENTER);
    }

    private void loadInitialData() {
        new SwingWorker<Void, Void>() {
            @Override
            protected Void doInBackground() {
                performSearch();
                return null;
            }
        }.execute();
    }

    private void performSearch() {
        try {
            DefaultTableModel model = new DefaultTableModel(
                    new String[]{"日期", "类型", "票据号", "患者ID", "金额"}, 0) {
                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                }
            };

            String feeType = (String) cbFeeType.getSelectedItem();

            // 使用参数包装对象
            SearchParams params = new SearchParams(
                    tfInvoiceNo.getText().trim(),
                    tfPatientId.getText().trim(),
                    tfStartDate.getText().trim(),
                    tfEndDate.getText().trim(),
                    feeType
            );

            new SwingWorker<Void, Void>() {
                List<Invoice> invoices = new ArrayList<>();

                @Override
                protected Void doInBackground() throws Exception {
                    // 并行查询
                    if (shouldSearchCharge(params)) {
                        invoices = new InvoiceDao().search(
                                params.invoiceNo,
                                parsePatientId(params.patientId),
                                params.startDate,
                                params.endDate
                        );
                    }

                    return null;
                }

                @Override
                protected void done() {
                    try {
                        // 添加收费记录
                        for (Invoice inv : invoices) {
                            model.addRow(new Object[]{
                                    inv.getCreateTime(),
                                    "收费",
                                    inv.getInvoiceNo(),
                                    inv.getPatientId(),
                                    String.format("+¥%.2f", inv.getAmount())
                            });
                        }

                        resultTable.setModel(model);
                        resultTable.setAutoCreateRowSorter(true);
                    } catch (Exception ex) {
                        handleError(ex);
                    }
                }
            }.execute();

        } catch (NumberFormatException ex) {
            UIUtil.showError(this, "患者ID必须为数字", "输入错误");
        } catch (Exception ex) {
            UIUtil.showError(this, "查询失败：" + ex.getMessage(), "系统错误");
        }
    }

    // 辅助方法
    private boolean shouldSearchCharge(SearchParams params) {
        return "全部".equals(params.feeType) || "收费".equals(params.feeType);
    }

    private boolean shouldSearchRefund(SearchParams params) {
        return "全部".equals(params.feeType) || "退费".equals(params.feeType);
    }

    private Integer parsePatientId(String input) {
        return input.isEmpty() ? null : Integer.parseInt(input);
    }

    private void handleError(Exception ex) {
        if (ex instanceof NumberFormatException) {
            UIUtil.showError(this, "患者ID必须为数字", "输入错误");
        } else {
            UIUtil.showError(this, "查询失败：" + ex.getMessage(), "系统错误");
        }
    }

    // 参数包装类
    private static class SearchParams {
        final String invoiceNo;
        final String patientId;
        final String startDate;
        final String endDate;
        final String feeType;

        SearchParams(String invoiceNo, String patientId,
                     String startDate, String endDate, String feeType) {
            this.invoiceNo = invoiceNo;
            this.patientId = patientId;
            this.startDate = startDate;
            this.endDate = endDate;
            this.feeType = feeType;
        }
    }

    private void resetCriteria() {
        tfPatientId.setText("");
        tfInvoiceNo.setText("");
        tfStartDate.setText("");
        tfEndDate.setText("");
        cbFeeType.setSelectedIndex(0);
        resultTable.setModel(new DefaultTableModel());
    }
}